package com.jumper.study.admin.service.impl;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumper.study.admin.service.LessonCategoryService;
import com.jumper.study.db.dao.LessonCategoryMapper;
import com.jumper.study.db.model.LessonCategory;

@Service
public class LessonCategoryServiceImpl extends ServiceImpl<LessonCategoryMapper, LessonCategory> implements LessonCategoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LessonCategoryServiceImpl.class);

    @Override
    public boolean hasChild(Integer id) {
        LambdaQueryWrapper<LessonCategory> wrapper = Wrappers.lambdaQuery(LessonCategory.class);
        wrapper.eq(LessonCategory::getParentId, id);
        return getBaseMapper().exists(wrapper);
    }
    
    @Override
    @Cacheable(value="category#600", key="#id", unless = "#result == null")
    public LessonCategory getById(Serializable id) {
        LOGGER.info("category service:{}", id);
        return super.getById(id);
    }

    @Override
    @CacheEvict(value="category", key="#entity.id")
    public boolean updateById(LessonCategory entity) {
        return super.updateById(entity);
    }

    @Override
    @CacheEvict(value="category", key="#id")
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
}
